//
//  LJMRankSub_sonerViewController.m
//  wind
//
//  Created by 李金明 on 16/1/12.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJMRankSub_sonerViewController.h"
#import "LJMRank_sonerSubModel.h"
#import "LJMRank_sonerCollectionViewCell.h"
#import "LJMRankSubRoot_SonerViewController.h"

//collectionfowl的坐标
#define COLLECTIONFOWLEDGE 5//边界
#define COLLECTWIDTH 10
@interface LJMRankSub_sonerViewController () <UICollectionViewDelegate, UICollectionViewDataSource, SubBackNavigationDelegate>
- (void)SubBackNavigationDelegate;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) UIButton *bactButton;
@property (nonatomic, retain) UIView *barview;
@property (nonatomic, retain) UILabel *titleLab;
@property (nonatomic, retain) UIActivityIndicatorView *testActivityIndicator;
@property (nonatomic, retain) UIApplication *application;

@end

@implementation LJMRankSub_sonerViewController
- (void)dealloc
{
    self.backDelegate = nil;
    [_testActivityIndicator release];
    [_application release];
    [_bactButton release];
    [_barview release];
    [_titleLab release];
    [_collectionV release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //设置navigatonbar
    [self navigationItemAction];
    //获取数据
    [self getData];
    //添加collectionView
    [self addCollectionView];
   
    //添加菊花转
    [self addUIActivityIndicatorView];

}
#pragma 添加collectionView
- (void)addCollectionView
{
    UICollectionViewFlowLayout *fowl = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    fowl.sectionInset = UIEdgeInsetsMake(COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE + 35, COLLECTIONFOWLEDGE);
    fowl.itemSize = CGSizeMake((self.view.frame.size.width - COLLECTWIDTH) / 3 , (self.view.frame.size.width - COLLECTWIDTH - 35) / 3 + COLLECTWIDTH);
    fowl.minimumLineSpacing = COLLECTIONFOWLEDGE;
    fowl.minimumInteritemSpacing = COLLECTIONFOWLEDGE;
    fowl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - COLLECTWIDTH) collectionViewLayout:fowl];
    
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionV];
    [_collectionV release];
    [self.collectionV registerClass:[LJMRank_sonerCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];


}
#pragma 增加个菊花转
- (void)addUIActivityIndicatorView
{
    //    //增加个菊花转
    self.testActivityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.testActivityIndicator.backgroundColor = [UIColor blackColor];
    self.testActivityIndicator.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 60) / 2 , [UIScreen mainScreen].bounds.size.height / 2 - 100, 60, 60);
    self.testActivityIndicator.layer.cornerRadius = 10;
    self.testActivityIndicator.layer.masksToBounds = YES;
    [self.barview addSubview:self.testActivityIndicator];
    [_testActivityIndicator release];
    [self.testActivityIndicator startAnimating];
    //增加网络旁边菊花转的方法
    self.application = [UIApplication sharedApplication];
    self.application.networkActivityIndicatorVisible = YES;
    
}
#pragma collectionView的协议方法
#pragma 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
#pragma cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJMRank_sonerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LJMRank_sonerSubModel *model = nil;
    if (nil != self.dataArr) {
        model = [self.dataArr objectAtIndex:indexPath.row];
    }
    
    
    
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.nameL.text = model.singer_name;
    return cell;
    
}
#pragma 点击的动作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
 LJMRank_sonerSubModel *model = [self.dataArr objectAtIndex:indexPath.row];
    LJMRankSubRoot_SonerViewController *subRootVC = [[LJMRankSubRoot_SonerViewController alloc] init];
    subRootVC.backDelegate = self;
    subRootVC.imageUrl = model.pic_url;
    subRootVC.singer_id = model.singer_id;
    subRootVC.singer_name = model.singer_name;
    [self.navigationController pushViewController:subRootVC animated:YES];
    [subRootVC release];
    [subRootVC.navigationItem setHidesBackButton:YES];
    [self.barview removeFromSuperview];
    
    
}
#pragma 获取数据
- (void)getData
{
[LJMNetworkTool getNetWithURL:self.urlStr body:nil headFile:nil responseStyle:LJMResponseJSON success:^(NSURLSessionDataTask *task, id responseObject) {
    self.dataArr = [NSMutableArray array];
    for (NSMutableDictionary *tempDic in [responseObject objectForKey:@"data"]) {
          LJMRank_sonerSubModel *model = [[LJMRank_sonerSubModel alloc] init];
        [model initWithDic:tempDic];
        [self.dataArr addObject:model];
        [model release];
    }
  
    
#pragma 缓存数据到本地
    NSString *path = [NSString stringWithFormat:@"%ld.plist", (long)[self.urlStr hash]];
    // 存储的沙盒路径
    NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 归档
    [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
    [self.collectionV reloadData];
    [self.testActivityIndicator stopAnimating];
    self.application.networkActivityIndicatorVisible = NO;
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    // 缓存的文件夹
    NSString *path = [NSString stringWithFormat:@"%ld.plist", (long)[self.urlStr hash]];
    NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    // 反归档
    id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
    
    if (result) {
        self.dataArr = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in [result objectForKey:@"data"]) {
            LJMRank_sonerSubModel *model = [[LJMRank_sonerSubModel alloc] init];
            [model initWithDic:tempDic];
            [self.dataArr addObject:model];
            [model release];
        }

            [self.collectionV reloadData];
        [self.testActivityIndicator stopAnimating];
        self.application.networkActivityIndicatorVisible = NO;
        
    } else {
        NSLog(@"error = %@", error);
    }

}];

}
#pragma 设置navigatonbar
- (void)navigationItemAction
{
    self.barview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, NAVIGATIONBARHEIGHT)];
    self.barview.userInteractionEnabled = YES;
    self.barview.backgroundColor = [UIColor colorWithRed:69 / 255.0 green:175 / 255.0 blue:248 / 255.0 alpha:1.0];
    [self.navigationController.navigationBar addSubview:self.barview];
    [_barview release];
    self.bactButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bactButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.bactButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bactButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.barview addSubview:self.bactButton];
    [self.bactButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.barview.mas_left).offset(10);
        make.width.mas_equalTo(@50);
        make.bottom.equalTo(self.barview.mas_bottom).offset(-5);
        make.height.mas_equalTo(@30);
    }];
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.text = self.titleStr;
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.font = [UIFont boldSystemFontOfSize:20];
    self.titleLab.textColor = [UIColor whiteColor];
    [self.barview addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.barview.mas_centerX);
        make.width.mas_equalTo(@200);
        make.centerY.equalTo(self.bactButton.mas_centerY);
        make.height.mas_equalTo(@30);
    }];

    
}
#pragma 返回按钮
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.barview removeFromSuperview];
    [self.backDelegate backNavigationDelegate];
    [self.testActivityIndicator stopAnimating];
    self.application.networkActivityIndicatorVisible = NO;
    
}
#pragma SubRoot_SonerVC中的协议动作
- (void)SubBackNavigationDelegate
{
    [self navigationItemAction];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
